<?php

class SectionsAction extends fvAction {
    
    function __construct () {
        parent::__construct(fvSite::$Layoult);
    }

    function executeIndex() {
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_NO_LAYOULT;
        else return self::$FV_OK;
    }
    
    function executeEdit() {
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_AJAX_CALL;
        else return self::$FV_OK;
    }
    
    function executeSave() {
        $request = fvRequest::getInstance();
        
        if (!$Section = SectionsManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $Section = new Sections();
        }
        $mn = $request->getRequestParameter('mn');
        
        if ($Section->isNew()) {
            $mn['menu_order'] = SectionsManager::getInstance()->getCount('parent_id = ?', array($mn['parent_id']));
        }
        
        $Section->updateFromRequest($mn);
        
        if ($Section->save()) {
            fvResponce::getInstance()->setHeader('Id', $Section->getPk());
            $this->setFlash("Данные успешно сохранены", self::$FLASH_SUCCESS);
            fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/?id=". $Section->getPk());        
        } else { 
            fvResponce::getInstance()->setHeader('X-JSON', json_encode($Section->getValidationResult()));
            $this->setFlash("Ошибка при сохранении данных проверте правильность введенных данных", self::$FLASH_ERROR);
        }
        
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_AJAX_CALL;
        else return self::$FV_OK;
    }

    private function getObjectById($array, $id) {
        foreach ($array as $key => $object) {
            if ($object->getPk() == $id) return $key;
        }
        return -1;
    }
    
    function executeReorder() {
        $Sections = SectionsManager::getInstance()->getAll();
        $sortableOrder = fvRequest::getInstance()->getRequestParameter("sortables");
        foreach ($sortableOrder as $menuOrder => $menuContent) {
            $menuId = $menuContent['id'];
            if (($menuKey = $this->getObjectById($Sections, $menuId)) != -1) {
                $Sections[$menuKey]->parent_id = 0;
                $Sections[$menuKey]->menu_order = $menuOrder;
            }
            if (count($menuContent) > 1) {
                $parentId = $menuContent['id'];
                unset($menuContent['id']);
                foreach ($menuContent as $menuOrder => $menuSubContent) {
                    $menuId = $menuSubContent['id'];
                    if (($menuKey = $this->getObjectById($Sections, $menuId)) != -1) {
                        $Sections[$menuKey]->parent_id = $parentId;
                        $Sections[$menuKey]->menu_order = $menuOrder;
                    }
                }
            }
        }
        foreach ($Sections as $menu) {
            $menu->save(false);
        }
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_AJAX_CALL;
        else return self::$FV_OK;
    }

    function executeDelete() {
        $request = fvRequest::getInstance();
        if (!$Section = SectionsManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $this->setFlash("Ошибка при удалении.", self::$FLASH_ERROR);
        } else {
            $Section->delete();
            $this->setFlash("Данные успешно удалены", self::$FLASH_SUCCESS);
        }
        
        fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_NO_LAYOULT;
        else return self::$FV_OK;
    }
    
}

?>
